package com.anrongtec.zcpt.tab03;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseFragment;
import com.anrongtec.zcpt.callback.StoreStringCallBack;
import com.anrongtec.zcpt.constant.Constant;
import com.anrongtec.zcpt.interfaces.HttpUtil;
import com.anrongtec.zcpt.interfaces.URLConstants;
import com.anrongtec.zcpt.interfaces.module.JsonDataRestult;
import com.anrongtec.zcpt.module.DataType;
import com.anrongtec.zcpt.module.Type;
import com.anrongtec.zcpt.ui.TypeSearchActivity;
import com.anrongtec.zcpt.util.GsonUtil;
import com.anrongtec.zcpt.util.Util;
import com.anrongtec.zcpt.view.ObservableScrollView;
import com.anrongtec.zcpt.view.ObservableScrollView.ScrollViewListener;
import com.art.base.util.ToastShow;
import com.art.base.util.log.LogUtil;
import com.google.gson.reflect.TypeToken;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 分类页面
 * @author cxy
 *
 */
public class TabThirdFragment extends BaseFragment implements ScrollViewListener, OnTouchListener{
	private ObservableScrollView scrollView;
	
	private RecyclerView rv_police, rv_place, rv_function;
	
	private SwipeRefreshLayout swipe_refresh;
	
	private List<Type> list_police, list_place, list_function;
	
	private LinearLayoutCompat ll_police, ll_place, ll_function;

	private CommonAdapter<Type> adapter, adapter_police, adapter_place, adapter_function;
	/**当前点击的type*/
	private Type clickType;
	/**分类的大类名称 如：警种分类(PoliceType)、地区分类(AreaType)、功能分类(BusType) */
	private String typeClass;
	
	private Intent intent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_tab_third, null);
		
		initViews(v);
		
		initData();
		
		return v;
	}
	
	@Override
	protected void initViews(View view) {
		scrollView = (ObservableScrollView) view.findViewById(R.id.osv_tab03);
//		scrollView.setScrollViewListener(this);
//		scrollView.setOnTouchListener(this);
		
		ll_police = (LinearLayoutCompat) view.findViewById(R.id.ll_tab03_police);
		ll_place = (LinearLayoutCompat) view.findViewById(R.id.ll_tab03_place);
		ll_function = (LinearLayoutCompat) view.findViewById(R.id.ll_tab03_function);
		initRecyclerView(view);
		
		initSwipeRefresh(view);
	}

	/**
	 * 初始化下拉刷新
	 * @param view 
	 */
	private void initSwipeRefresh(View view) {
		swipe_refresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_tab03_refresh);
		swipe_refresh.setColorSchemeResources(
				android.R.color.holo_blue_bright,
				android.R.color.holo_orange_light,
				android.R.color.holo_green_light);
		swipe_refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
//				swipe_refresh.setRefreshing(true);
				getTypes();
			}
		});
	}
	
	/**
	 * 初始化recyclerview
	 * @param v
	 */
	private void initRecyclerView(View v) {
		rv_police = (RecyclerView) v.findViewById(R.id.recyclerview_tab03_police);
		rv_police.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false));
		
		rv_place = (RecyclerView) v.findViewById(R.id.recyclerview_tab03_place);
		rv_place.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false));
		
		rv_function = (RecyclerView) v.findViewById(R.id.recyclerview_tab03_function);
		rv_function.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false));
		
	}

	private void setAdapter() {
		if (!Util.isEmpty(list_police) && list_police.size() != 0) {
			ll_police.setVisibility(View.VISIBLE);
			adapter_police = getAdapter(list_police);
			adapter_police.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
					clickType = list_police.get(position);
					typeClass = URLConstants.APP_TYPE_POLICE;
					toSearchActivity();
				}

				@Override
				public boolean onItemLongClick(View view, android.support.v7.widget.RecyclerView.ViewHolder holder,
						int position) {
					// TODO Auto-generated method stub
					return false;
				}
			});
			rv_police.setAdapter(adapter_police);
		}
		if (!Util.isEmpty(list_place) && list_place.size() != 0) {
			ll_place.setVisibility(View.VISIBLE);
			adapter_place = getAdapter(list_place);
			adapter_place.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
					clickType = list_place.get(position);
					typeClass = URLConstants.APP_TYPE_AREA;
					toSearchActivity();
				}

				@Override
				public boolean onItemLongClick(View view, android.support.v7.widget.RecyclerView.ViewHolder holder,
						int position) {
					// TODO Auto-generated method stub
					return false;
				}
			});
			rv_place.setAdapter(adapter_place);
		}
		if (!Util.isEmpty(list_function) && list_function.size() != 0) {
			ll_function.setVisibility(View.VISIBLE);
			adapter_function = getAdapter(list_function);
			adapter_function.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
					clickType = list_function.get(position);
					typeClass = URLConstants.APP_TYPE_BUS;
					toSearchActivity();
				}

				@Override
				public boolean onItemLongClick(View view, android.support.v7.widget.RecyclerView.ViewHolder holder,
						int position) {
					// TODO Auto-generated method stub
					return false;
				}
			});
			rv_function.setAdapter(adapter_function);
		}
	}

	
	/**
	 * 跳转到搜索页面
	 */
	protected void toSearchActivity() {
		intent = new Intent(getActivity(), TypeSearchActivity.class);
		intent.putExtra(Constant.DATA, clickType);
		intent.putExtra(Constant.TYPE, typeClass);
		startActivity(intent);
	}

	/**
	 * 初始化数据
	 */
	@Override
	protected void initData() {

		list_police = new ArrayList<Type>();
		list_place = new ArrayList<Type>();
		list_function = new ArrayList<Type>();
		
		getTypes();
		
	}

	/**
	 * 获取分类列表
	 */
	private void getTypes() {
		showRefresh(true);
		String json = GsonUtil.toTypeQueryJson();
		HttpUtil.post(json, new StoreStringCallBack(getActivity()) {
			@Override
			public void onResponse(boolean arg0, String arg1, Request arg2, Response arg3) {
				super.onResponse(arg0, arg1, arg2, arg3);
				showRefresh(false);
				JsonDataRestult<DataType> result = GsonUtil.decode(arg1, new TypeToken<JsonDataRestult<DataType>>(){}.getType());
				if (URLConstants.RetCode.SUCCESS.equals(result.getRetCode())) {
					list_police = result.getData().getPoliceType();
					list_place = result.getData().getAreaType();
					list_function = result.getData().getBusType();
					setAdapter();
				}else {
					ToastShow.shortMessage(result.getRetMsg());
				}
			}
			
			@Override
			public void onError(boolean isFromCache, Call call,
					Response response, Exception e) {
				setAdapter();
				showRefresh(false);
				super.onError(isFromCache, call, response, e);
			}
		});
	}

	/**
	 * 设置recyclerview的adapter
	 * @param list 
	 * @param rv 
	 * @return 
	 */
	private CommonAdapter<Type> getAdapter(List<Type> list) {
		CommonAdapter<Type> commonAdapter = new CommonAdapter<Type>(getContext(), R.layout.item_tab_third, list) {
			@Override
			protected void convert(ViewHolder holder, Type t, int position) {
				holder.setText(R.id.tv_tab03_type, t.getName());
			}
		};
		return commonAdapter;

	}
	
	
	/**
	 * 是否显示refresh
	 * @param b
	 */
	private void showRefresh(final boolean b){
		swipe_refresh.post(new Runnable() {
			@Override
			public void run() {
				swipe_refresh.setRefreshing(b);
			}
		});
	}

	private int currentY;
	private boolean isFirstMove = true;
	
	/**
	 * scrollView 监听
	 */
	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
		currentY = y;
	}

	/**
	 * 
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			if (isFirstMove) {
				isFirstMove = false;
				LogUtil.trace(Log.DEBUG, "touch", currentY+"");
				if (currentY == 0) {
					swipe_refresh.setEnabled(true);
				}else {
					swipe_refresh.setEnabled(false);
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			LogUtil.trace(Log.DEBUG, "touch UP", currentY+"");
			isFirstMove = true;
			break;
		default:
			break;
		}
		
		
		return false;
	}
	
	
}
